#/bin/bash

:<<description
功能：Git
描述：

description


#设置脚本异常立即退出
set -e
if [[ ${debug} == 'T' ]]; then set -x; fi
#获取当前脚本名
script=$0
scriptDir=$(cd $(dirname $0); pwd)
#导入通用函数
for file in $(ls ${workDir}/function*.sh); do source ${file}; done
#获取输入参数并命名
action=$1
targetResource=$2
sequence=$3
executeProfile=$4
host=$5
user=$6
#校验命名参数，建议使用正则
#if [[ ! "${action}" ]]; then logger "" "error"; exit 1; fi
#if [[ ! "${resource}" ]]; then logger "" "error"; exit 1; fi
#if [[ ! "${sequence}" ]]; then logger "" "error"; exit 1; fi
#if [[ ! "${executeProfile}" ]]; then logger "" "error"; exit 1; fi
#if [[ ! "${host}" ]]; then logger "" "error"; exit 1; fi
#if [[ ! "${user}" ]]; then logger "" "error"; exit 1; fi
#倒入配置
targetResourceProfile=${workDir}/manager/default-manager/profile.sh
if [[ -f ${targetResourceProfile} ]]; then source ${targetResourceProfile}; fi
targetResourceProfile=${workDir}/manager/${targetResource}-manager/profile.sh
if [[ -f ${targetResourceProfile} ]]; then source ${targetResourceProfile}; fi
targetResourceProfile=${workDir}/manager/${targetResource}-manager/profile-${executeProfile}.sh
if [[ -f ${targetResourceProfile} ]]; then source ${targetResourceProfile}; fi
#其他参数



#判断必须参数非空


function gitClone() {
    if [ ! -d ${gitRepoHub}/${sequence}/${gitRepoHome} ]
        then
            mkdir -p ${gitRepoHub}/${sequence}
            cmd="git clone -b ${gitRepoBranch} ${gitRepoUrl} ${gitRepoHub}/${sequence}/${gitRepoHome}"
            logger "${barRightArrow30}"
            logger "${cmd}"
            logger "${barLeftArrow30}"
            ${cmd}
        else
            logger "Dir already exits ${gitRepoRoot}"
            exit 1
    fi
}

function gitCloneForce() {
    if [ -d ${gitRepoHub}/${sequence}/${gitRepoHome} ]; then
        rm -rf ${gitRepoHub}/${sequence}/${gitRepoHome}
    fi
    gitClone
}

function gitDiff(){

    if [ ! -d ${gitRepoHub}/${sequence}/${gitRepoHome}  ];  then
        logger "Dir not found ${gitRepoHub}/${sequence}/${gitRepoHome} "
        return 1
    fi
    cd ${gitRepoHub}/${sequence}/${gitRepoHome}

    lastTag=${gitRepoBranchLastTime}
    thisTag=${gitRepoBranch}
    logger "Notice ${thisTag} is remotes/${gitRemoteRepoAlias}/${thisTag}"
    logger "Notice ${lastTag} is remotes/${gitRemoteRepoAlias}/${lastTag}"
    cmd="git diff remotes/origin/${lastTag} remotes/origin/${thisTag}"
    logger "${cmd}"
    ${cmd} | grep 'diff --git' | awk '{print $3}' | sed -r -n 's|a/(.*)|\1|p' | tee git_diff_${lastTag}_${thisTag}.lst

}

#function sshGitClone () {
#
#    set +e
#    ssh ${user}@${host} "
#    action=${action}
#    targetResource=${targetResource}
#    sequence=${sequence}
#    executeProfile=${executeProfile}
#    gitRepoHub=${gitRepoHub}
#    gitRepoHome=${gitRepoHome}
#    gitRepoUrl=${gitRepoUrl}
#    gitRepoBranch=${gitRepoBranch}
#    gitRemoteRepoAlias=${gitRemoteRepoAlias}
#    barRightArrow30='${barRightArrow30}'
#    barLeftArrow30='${barLeftArrow30}'
#    $(typeset -f checkIpAddress)
#    $(typeset -f checkUser)
#    $(typeset -f checkRevoker)
#    $(typeset -f logger)
#    $(typeset -f gitClone)
#    gitClone
#    "
#    if [[ ! $? == 0 ]]; then return 1; logger "Ssh gitClone failed." "error"; fi
#    logger "Ssh gitClone succeed."
#    set -e
#    return 0
#}
#
#function sshGitCloneForce () {
#
#    set +e
#    ssh ${user}@${host} "
#    action=${action}
#    targetResource=${targetResource}
#    sequence=${sequence}
#    executeProfile=${executeProfile}
#    gitRepoHub=${gitRepoHub}
#    gitRepoHome=${gitRepoHome}
#    gitRepoUrl=${gitRepoUrl}
#    gitRepoBranch=${gitRepoBranch}
#    gitRemoteRepoAlias=${gitRemoteRepoAlias}
#    barRightArrow30='${barRightArrow30}'
#    barLeftArrow30='${barLeftArrow30}'
#    $(typeset -f checkIpAddress)
#    $(typeset -f checkUser)
#    $(typeset -f checkRevoker)
#    $(typeset -f logger)
#    $(typeset -f gitClone)
#    $(typeset -f gitCloneForce)
#    gitCloneForce
#    "
#    if [[ ! $? == 0 ]]; then return 1; logger "Ssh gitCloneForce failed." "error"; fi
#    logger "Ssh gitCloneForce succeed."
#    set -e
#    return 0
#}
#
#function sshGitDiff () {
#
#    set +e
#    ssh ${user}@${host} "
#    action=${action}
#    targetResource=${targetResource}
#    sequence=${sequence}
#    executeProfile=${executeProfile}
#    gitRepoHub=${gitRepoHub}
#    gitRepoHome=${gitRepoHome}
#    gitRepoUrl=${gitRepoUrl}
#    gitRepoBranch=${gitRepoBranch}
#    gitRemoteRepoAlias=${gitRemoteRepoAlias}
#    barRightArrow30='${barRightArrow30}'
#    barLeftArrow30='${barLeftArrow30}'
#    $(typeset -f checkIpAddress)
#    $(typeset -f checkUser)
#    $(typeset -f checkRevoker)
#    $(typeset -f logger)
#    $(typeset -f gitDiff)
#    gitDiff
#    "
#    if [[ ! $? == 0 ]]; then return 1; logger "Ssh gitDiff failed." "error"; fi
#    logger "Ssh gitDiff succeed."
#    set -e
#    return 0
#}


#执行
#case ${action} in
#    'gitClone') gitClone; ;;
#    'gitCloneForce') gitCloneForce; ;;
#    'gitDiff') gitDiff; ;;
#    'sshGitClone') sshCheck; sshGitClone; ;;
#    'sshGitCloneForce') sshCheck; sshGitCloneForce; ;;
#    'sshGitDiff') sshCheck; sshGitDiff; ;;
#    *) logger "Unknown action."; exit 1; ;;
#esac
case ${action} in
    'gitClone') gitClone; ;;
    'gitCloneForce') gitCloneForce; ;;
    'gitDiff') gitDiff; ;;
    *) logger "Unknown action ${action}."; exit 1; ;;
esac


if [[ ! $? == 0 ]]; then
    logger "${action^} ${targetResource} on ${host:-local} with ${user:-$(whoami)} when ${executeProfile} ${sequence} failed" "error"
    exit 1
fi
logger "${action^} ${targetResource} on ${host:-local} with ${user:-$(whoami)} when ${executeProfile} ${sequence} succeed"


